#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

struct Student {
    string studentID;
    string name;
    double mathScore;
    double chineseScore;

    // 重载 >> 运算符
    friend istream &operator>>(istream &is, Student &s) {
        is >> s.studentID >> s.name >> s.mathScore >> s.chineseScore;
        return is;
    }

    // 重载 << 运算符
    friend ostream &operator<<(ostream &os, const Student &s) {
        os << s.studentID << " " << s.name << " " << s.mathScore << " " << s.chineseScore;
        return os;
    }
};

class Menu {
private:
    vector<Student> students;

public:
    void display_menu() {
        cout << "1. 从文件students.txt导入学生信息\n";
        cout << "2. 键入以添加学生信息\n";
        cout << "3. 计算平均成绩，并存入stud.txt\n";
        cout << "4. 输出高于平均成绩的学生，并存入stud.txt\n";
        cout << "5. 移除成绩不及格学生，并存入bad.txt\n";
        cout << "6. 以姓名搜索学生\n";
        cout << "7. 退出\n";
    }

    void load_students_from_file(string filename);

    void input_new_student_info();

    void calculate_average_scores();

    void display_above_average_students_from_file();

    void remove_students_with_failing_grades();

    void search_student_by_name(string name);

    void run();
};

void Menu::load_students_from_file(string filename) {
    ifstream inFile(filename);
    if (!inFile.is_open()) {
        cerr << "Error opening file: " << filename << endl;
        return;
    }

    students.clear();
    Student student;
    while (inFile >> student) {
        students.push_back(student);
    }

    inFile.close();
    cout << "Loaded " << students.size() << " students from file." << endl;
}

void Menu::input_new_student_info() {
    Student newStudent;
    cout << "键入学号: ";
    cin >> newStudent.studentID;
    cout << "键入姓名: ";
    cin >> newStudent.name;
    cout << "键入数学成绩: ";
    cin >> newStudent.mathScore;
    cout << "键入语文成绩: ";
    cin >> newStudent.chineseScore;
    students.push_back(newStudent);
    cout << "您添加了一条新的学生信息" << endl;
}

void Menu::calculate_average_scores() {
    if (students.empty()) {
        cout << "无此数据" << endl;
        return;
    }
    double mathSum = 0.0;
    double chineseSum = 0.0;
    for (const auto &student : students) {
        mathSum += student.mathScore;
        chineseSum += student.chineseScore;
    }
    double mathAverage = mathSum / students.size();
    double chineseAverage = chineseSum / students.size();
    double overallAverage = (mathAverage + chineseAverage) / 2;

    ofstream outFile("stud.txt");
    if (!outFile) {
        cout << "打开文件保存平均分数时出错" << endl;
        return;
    }
    for (const auto &student : students) {
        double studentAverage = (student.mathScore + student.chineseScore) / 2;
        if (studentAverage >= overallAverage) {
            outFile << student << " " << studentAverage << endl;
        }
    }
    outFile.close();
    cout << "计算平均分数并保存为了“stud.txt”" << endl;
}

void Menu::display_above_average_students_from_file() {
    ifstream file("stud.txt");
    if (!file) {
        cout << "打开文件出错" << endl;
        return;
    }
    cout << "学号 姓名 数学成绩 语文成绩 平均分" << endl;
    string line;
    while (getline(file, line)) {
        cout << line << endl;
    }
    file.close();
}

void Menu::remove_students_with_failing_grades() {
    if (students.empty()) {
        cout << "无可用学生信息" << endl;
        return;
    }
    vector<Student> failingStudents;
    vector<Student> passingStudents;
    for (const auto &student : students) {
        if (student.mathScore < 60 && student.chineseScore < 60) {
            failingStudents.push_back(student);
        } else {
            passingStudents.push_back(student);
        }
    }
    ofstream outFile("bad.txt");
    if (!outFile) {
        cout << "保存不及格学生信息时，打开文件bad.txt出错" << endl;
        return;
    }
    for (const auto &student : failingStudents) {
        outFile << student << endl;
    }
    outFile.close();
    students = passingStudents;
    cout << "成绩不及格的学生已经被移除并且另存到了'bad.txt'中." << endl;
}

void Menu::search_student_by_name(string name) {
    bool found = false;
    for (const auto &student : students) {
        if (student.name == name) {
            cout << "找到学生:" << endl;
            cout << "学号: " << student.studentID << endl;
            cout << "姓名: " << student.name << endl;
            cout << "数学成绩: " << student.mathScore << endl;
            cout << "语文成绩: " << student.chineseScore << endl;
            found = true;
            break;
        }
    }
    if (!found) {
        cout << "查无此人： " << name << "." << endl;
    }
}

void Menu::run() {
    int choice;
    do {
        display_menu();
        cout << "===== 键入以选择 (1-7): ";
        cin >> choice;
        switch (choice) {
            case 1: {
                string filename;
                cout << "键入文件名（同级目录下）: ";
                cin >> filename;
                load_students_from_file(filename);
                break;
            }
            case 2:
                input_new_student_info();
                break;
            case 3:
                calculate_average_scores();
                break;
            case 4:
                display_above_average_students_from_file();
                break;
            case 5:
                remove_students_with_failing_grades();
                break;
            case 6: {
                string name;
                cout << "键入姓名: ";
                cin >> name;
                search_student_by_name(name);
                break;
            }
            case 7:
                cout << "退出..." << endl;
                break;
            default:
                cout << "不合法输入，请重试" << endl;
        }
    } while (choice != 7);
}

int main() {
    Menu menu;
    menu.run();
    return 0;
}
